/**
 * Created by Eoly on 2017/2/16.
 * Call http://palerock.cn
 */
var addressPage = angular.module('addressPage', ['ngRoute']);



/**
 * 地址页面
 */
addressPage.controller('address', function ($scope, $http) {
    $scope.suiMobileInit = function () {
        app.suiInit();
        app.adaptPhone();
    };

    //获取的用户地址列表
    $scope.addresses = [];

    //获取用户地址列表
    $scope.getAddresses = function () {
        var token = app.getUserToken();

        if (!token) {
            return;
        }

        if (token) {
            $.showPreloader('请稍候');
            $http({
                method: 'get',
                url: net + '/user/show-address',
                headers: {'Authorization': 'Bearer ' + token}
            }).then(function (res) {
                console.log(res);
                $.hidePreloader();
                if (!res.data.error) {
                    $scope.addresses = res.data.dataList;
                } else {
                    $.alert("错误代码:" + res.data.error + "," + res.data.mes)
                }
            }, function () {
                $.hidePreloader();
                $.alert('网络或设置错误');
            })
        }
    };

    //保存欲编辑的用户地址信息
    $scope.saveEditId = function (address) {
        var addressJson = JSON.stringify(address);
        sessionStorage.setItem('ptsc_pre_edit_address', addressJson);
        $scope.editAddress = address;
    };

    $scope.getEditInfo = function () {
        var editAddress = sessionStorage.getItem('ptsc_pre_edit_address');
        if (!editAddress) {
            app.backLastPosition();
        }
        $scope.editAddress = JSON.parse(editAddress);
    };

    $scope.provinceList = [];
    $scope.province = {};

    $scope.cityList = [];
    $scope.city = {};

    $scope.countyList = [];
    $scope.county = {};

    $scope.showChina = function (type) {
        var pid = 0;
        switch (type) {
            case 'province':
                break;
            case 'city':
                $scope.countyList = [];
                $scope.county = {};
                if ($scope.province == null || $scope.province.id == null) return;
                pid = $scope.province.id;
                break;
            case 'county':
                if ($scope.city == null || $scope.city.id == null) return;
                pid = $scope.city.id;
                break;
            default:
                return;
        }
        var param = {};
        param.pid = pid;
        $.showIndicator();
        $http({
            url: 'http://palerock.cn/api/show-china',
            params: param
        }).then(
            function (res) {
                console.log(res);
                $.hideIndicator();
                if (!res.data.error) {
                    $scope[type + 'List'] = res.data.dataList;
                } else {
                    if (res.data.error == 55) {
                        return;
                    }
                    $.alert("错误代码:" + res.data.error + "," + res.data.mes)
                }
            }, function () {
                $.hideIndicator();
                $.alert('网络或设置错误');
            }
        );
    };

    $scope.$watch('province', function () {
        $scope.showChina('city');
    });

    $scope.$watch('city', function () {
        $scope.showChina('county');
    });

    $scope.addressDetail = '';
    $scope.handEditAddress = function () {
        //拼接地址
        var addressInfo =
            ($scope.province.name ? $scope.province.name : '') +
            ($scope.city.name ? $scope.city.name : '') +
            ($scope.county.name ? $scope.county.name : '') +
            $scope.addressDetail;
        if (addressInfo != '') {
            $scope.editAddress.addressInfo = addressInfo;
        }

        //请求修改地址
        var token = app.getUserToken();

        if (!token) {
            return;
        }

        $.showPreloader('请稍候');
        $http({
            method: 'post',
            url: net + '/user/edit-address',
            headers: {'Authorization': 'Bearer ' + token},
            data: $scope.editAddress
        }).then(function (res) {
            console.log(res);
            $.hidePreloader();
            if (!res.data.error) {
                //回到获取地址列表刷新
                $.alert('修改成功', function () {
                    app.backLastPosition();
                });
            } else {
                $.alert("错误代码:" + res.data.error + "," + res.data.mes)
            }
        }, function () {
            $.hidePreloader();
            $.alert('网络请求失败，请检查网络连接');
        });
    };

    //设定默认地址信息
    $scope.addressDefault = {};
    $scope.$watch('addressDefault', function () {

        //储存到缓存区
        var addressJson = JSON.stringify($scope.addressDefault);
        if (addressJson == '{}') {
            return;
        }
        localStorage.setItem('ptsc_default_address', addressJson);
    });

    //获取默认地址信息
    $scope.getDefaultInfo = function () {
        var addressDefault = localStorage.getItem('ptsc_default_address');
        if (!addressDefault) {
            return;
        }
        $scope.addressDefault = JSON.parse(addressDefault);
    };

    $scope.addAddress = {};

    //添加地址
    $scope.handAddAddress = function () {
        //拼接地址
        var addressInfo =
            ($scope.province.name ? $scope.province.name : '') +
            ($scope.city.name ? $scope.city.name : '') +
            ($scope.county.name ? $scope.county.name : '') +
            $scope.addressDetail;
        if (addressInfo == '') {
            return;
        }
        $scope.addAddress.addressInfo = addressInfo;

        //请求添加地址
        var token = app.getUserToken();

        if (!token) {
            return;
        }

        $.showPreloader('请稍候');
        $http({
            method: 'post',
            url: net + '/user/add-address',
            headers: {'Authorization': 'Bearer ' + token},
            data: $scope.addAddress
        }).then(function (res) {
            console.log(res);
            $.hidePreloader();
            if (!res.data.error) {
                //回到获取地址列表刷新
                $.alert('添加成功', function () {
                    app.backLastPosition();
                });
            } else {
                $.alert("错误代码:" + res.data.error + "," + res.data.mes)
            }
        }, function () {
            $.hidePreloader();
            $.alert('网络请求失败，请检查网络连接');
        });
    };
});

/**
 * 路由
 */
addressPage.config(function ($routeProvider) {
    $routeProvider.when('/list', {
        templateUrl: 'html/address-list-content.html',
        controller: 'address'
    }).when('/edit', {
        templateUrl: 'html/address-edit-content.html',
        controller: 'address'
    }).when('/add', {
        templateUrl: 'html/address-add-content.html',
        controller: 'address'
    }).otherwise({
        redirectTo: '/list'
    });
});